package com.phoenix.controller;

import com.phoenix.entity.*;
import com.phoenix.service.GroupMemberService;
import com.phoenix.service.UserMemberService;
import com.phoenix.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CommonController {
    @Autowired
    UserService userService;
    @Autowired
    UserMemberService userMemberService;
    @Autowired
    GroupMemberService groupMemberService;

    @GetMapping("/")
    public String mainPage() {
        return "control/login";
    }

    @GetMapping("/login")
    public String login() {
        return "control/login";
    }

    @GetMapping("/register")
    public String register() {
        return "control/register";
    }

    @GetMapping("/welcome")
    public String welcome() {
        return "control/welcome";
    }

    @GetMapping("/userWelcome")
    public String userWelcome() {
        return "user/userWelcome";
    }

    @GetMapping("/error")
    public String error() {
        return "error";
    }

    //系统登出
    @RequestMapping("/logout")
    public String logout() {
        //调用Shiro清除token
        SecurityUtils.getSubject().logout();
        return "control/login";
    }

    @GetMapping("/editUser")
    public String editUser(Model model, String userID) {
        Users user = userService.selectUserByID(userID);
        model.addAttribute("user", user);
        return "admin/adminEdit";
    }

    //区分用于权限管理
    @GetMapping("/editSelf")
    public String editSelf(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        Users user = userService.selectUserByUsername(userName);
        model.addAttribute("user", user);
        return "user/userEdit";
    }

    @GetMapping("/adminAdd")
    public String adminAdd() {
        return "admin/adminAdd";
    }

    @GetMapping("/memberInfo")
    public String memberInfo(Model model) {
        UserMember userMember = new UserMember();
        model.addAttribute("userMember", userMember);
        return "user/memberInfo";
    }

    @GetMapping("/groupInfo")
    public String groupInfo(Model model) {
        GroupMember groupMember = new GroupMember();
        model.addAttribute("groupMember", groupMember);
        return "user/groupInfo";
    }

    @GetMapping("/editMember")
    public String editMember(Model model, String infoID) {
        UserMember userMember = userMemberService.selectMemberByID(infoID);
        model.addAttribute("userMember", userMember);
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        Users user = userService.selectUserByUsername(userName);
        if (user.getRole().equals("admin")) {
            return "admin/approvalMemberInfo";
        } else {
            return "user/memberInfo";
        }
    }

    @GetMapping("/editGroup")
    public String editGroup(Model model, String groupID) {
        GroupMember groupMember = groupMemberService.selectGroupByID(groupID);
        model.addAttribute("groupMember", groupMember);
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        Users user = userService.selectUserByUsername(userName);
        if (user.getRole().equals("admin")) {
            return "admin/approvalGroupInfo";
        } else {
            return "user/groupInfo";
        }
    }

    @GetMapping("/memberChampionships")
    public String memberChampionships(Model model) {
        MemberChampionship memberChampionship = new MemberChampionship();
        model.addAttribute("memberChampionship", memberChampionship);
        model.addAttribute("method", "posts");
        return "championships/memberChampionships";
    }

    @GetMapping("/groupChampionships")
    public String groupChampionships(Model model) {
        GroupChampionship groupChampionship = new GroupChampionship();
        model.addAttribute("groupChampionship", groupChampionship);
        model.addAttribute("method", "post");
        return "championships/groupChampionships";
    }
}
